package com.ruoyi.web.controller.fenxi;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ROrg;
import com.ruoyi.system.domain.RiskTypeData;
import com.ruoyi.system.service.IROrgService;
import com.ruoyi.system.service.IRiskTypeDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 风险种类Controller
 * 
 * @author ruoyi
 * @date 2022-10-12
 */
@Controller
@RequestMapping("/system/type")
public class RiskTypeDataController extends BaseController
{
    private String prefix = "system/type";

    @Autowired
    private IRiskTypeDataService riskTypeDataService;

    @Autowired
    private IROrgService rOrgService;

    @RequiresPermissions("system:type:view")
    @GetMapping()
    public String type()
    {
        return prefix + "/type";
    }

    /**
     * 查询风险种类列表
     */
    @RequiresPermissions("system:type:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RiskTypeData riskTypeData)
    {
        startPage();
        List<RiskTypeData> list = riskTypeDataService.selectRiskTypeDataList(riskTypeData);
        return getDataTable(list);
    }

    /**
     * 导出风险种类列表
     */
    @RequiresPermissions("system:type:export")
    @Log(title = "风险种类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RiskTypeData riskTypeData)
    {
        List<RiskTypeData> list = riskTypeDataService.selectRiskTypeDataList(riskTypeData);
        ExcelUtil<RiskTypeData> util = new ExcelUtil<RiskTypeData>(RiskTypeData.class);
        return util.exportExcel(list, "风险种类数据");
    }

    /**
     * 新增风险种类
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 清除EVENT_KEY所有缓存
     */
    public  void clearEventKey(){
        List<ROrg> rOrgs = rOrgService.selectROrgList(new ROrg());
        for (ROrg rOrg : rOrgs) {
            CacheUtils.remove(CacheUtils.EVENT_KEY+rOrg.getId());
        }
    }
    /**
     * 新增保存风险种类
     */
    @RequiresPermissions("system:type:add")
    @Log(title = "风险种类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RiskTypeData riskTypeData)
    {
        clearEventKey();
        return toAjax(riskTypeDataService.insertRiskTypeData(riskTypeData));
    }

    /**
     * 修改风险种类
     */
    @RequiresPermissions("system:type:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        RiskTypeData riskTypeData = riskTypeDataService.selectRiskTypeDataById(id);
        mmap.put("riskTypeData", riskTypeData);
        return prefix + "/edit";
    }

    /**
     * 修改保存风险种类
     */
    @RequiresPermissions("system:type:edit")
    @Log(title = "风险种类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RiskTypeData riskTypeData)
    {
        clearEventKey();
        return toAjax(riskTypeDataService.updateRiskTypeData(riskTypeData));
    }

    /**
     * 删除风险种类
     */
    @RequiresPermissions("system:type:remove")
    @Log(title = "风险种类", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        clearEventKey();
        return toAjax(riskTypeDataService.deleteRiskTypeDataByIds(ids));
    }
}
